<?php
class Controller_Bluebaroffer extends Controller_Abstract
{
	public function actionIndex() {
		$blueBarOfferModel = new BlueBarOffer();
		$this->_view['blueBarOffers'] = $blueBarOfferModel->getAllBlueBarOffers();
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
	}
	
	public function actionUpdate() {
		$blueBarOfferModel = new BlueBarOffer();
		if ($this->_context->isGET() && isset($this->_context->id)) {
			$blueBarOffer = $blueBarOfferModel->getBlueBarOfferById($this->_context->id);
			if ($blueBarOffer) {
				$this->_view['blueBarOffer'] = $blueBarOffer;
				$this->_view['sites'] = DataUtil::getOptionsForSites();
			}
		} else {
			$id = $this->_context->post('id');
			$siteId = $this->_context->post('site_id');
			$PartnerName = $this->_context->post('partner_name', '');
			$offerText = $this->_context->post('offer_text', '');
			$credit = $this->_context->post('credit', 0);
			$link = $this->_context->post('link', '');
			$ord = $this->_context->post('order', 1);
			$isPartial = $this->_context->post('is_partial', 0);

			if ($blueBarOfferModel->updateBlueBarOffer($siteId, $PartnerName, $offerText, $credit, $id, $link, $ord, $isPartial)) {
				$infos = array();
				$infos[] = Util::getMessage(15002);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('bluebaroffer/index'));
			}
		}
	}
	
	public function actionDelete() {
		$blueBarOfferModel = new BlueBarOffer();
		$blueBarOfferModel->deleteBlueBarOffer($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(15003);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('bluebaroffer/index'));
	}
	
	public function actionPause() {
		$blueBarOfferModel = new BlueBarOffer();
		$blueBarOfferModel->pauseBlueBarOffer($this->_context->id);
		return $this->_redirect(url('bluebaroffer/index'));
	}
	
	public function actionAdd() {
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if ($this->_context->isPOST()) {
			$siteId = $this->_context->post('site_id');
			$PartnerName = $this->_context->post('partner_name', '');
			$offerText = $this->_context->post('offer_text', '');
			$credit = $this->_context->post('credit', 0);
			$link = $this->_context->post('link', '');
			$ord = $this->_context->post('order', 1);
			$isPartial = $this->_context->post('is_partial', 0);
			
			$blueBarOfferModel = new BlueBarOffer();
			if ($blueBarOfferModel->addBlueBarOffer($siteId, $PartnerName, $offerText, $credit, $link, $ord, $isPartial)) {
				$infos = array();
				$infos[] = Util::getMessage(15001);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('bluebaroffer/index'));
			}
		}
	}
	
	
	public function actionChangeord() {
		if($this->_context->isPOST()){
			$btn = $this->_context->btn;
			$bluebarId = $this->_context->bid;
			
			$blueBarOfferModel = new BlueBarOffer();
			$result = $blueBarOfferModel->changeBlueBarOfferOrd($bluebarId, $btn);
			if($result){
				return "$result";
			}else{
				return 'false';
			}
		}
	}
}